package com.algorithm.one2fifty;

/**
 * @author: tzj
 * @create: 2021-05-30
 **/
public class LongestCommonPrefix_14 {

    public static void main(String[] args) {
        String[] strs = {"flower","flow","flight"};
        System.out.println(longestCommonPrefix(strs));
    }

    public static String longestCommonPrefix(String[] strs) {
        if (strs==null || strs.length==0){
            return "";
        }

        int len = strs[0].length();
        int count = strs.length;
        for (int i = 0; i < len; i++) {
            // 以第一个字符串为基础，一列一列比较
            char charAt = strs[0].charAt(i);
            // 从第二列开始的每个字符开始比较，依次类推
            for (int j = 1; j < count; j++) {
                // 如果后面的某个字符串的长度只是第一个的第一部分，那就截取到此
                if (strs[j].length()==i || strs[j].charAt(i)!=charAt){
                    return strs[0].substring(0, i);
                }
            }
        }
        return strs[0];
    }
}
